﻿using Aliyun.OSS;
using Aliyun.OSS.Util;
using CanDoo.Filter;
using CanDoo.Oil.Core.DTOs;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Deployment.Application;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CanDooOSS
{
    public partial class Form1 : Form
    {

#if DEBUG
        private string ServerBaseAddress = System.Configuration.ConfigurationManager.AppSettings["API_URL_TEST"].ToString();
#else
        private string ServerBaseAddress = System.Configuration.ConfigurationManager.AppSettings["API_URL"].ToString();
#endif

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //  /api/Media
            // 获取当前部署
            this.ed_md5.ReadOnly = true;
            this.ed_size.ReadOnly = true;
#if DEBUG
            var ver = "1.0.0.*";// 取得版本
#else
            ApplicationDeployment appd = ApplicationDeployment.CurrentDeployment;
            var ver = appd.CurrentVersion;// 取得版本号
#endif
            this.Text = "塔石售油机媒体上传工具V" + ver + $"  api地址：{ServerBaseAddress}";
            this.ed_endPlayTime.Value = DateTime.Now.AddMonths(1);
        }

        private void btnUpload_Click(object sender, EventArgs e)
        {
            if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                var fileToUpload = this.openFileDialog1.FileName;
                this.ed_name.Text = Path.GetFileNameWithoutExtension(this.openFileDialog1.FileName);

                Task.Run(()=> {
                    ShowUploadStatus(0.00);
                    var md5 = CanDoo.Secutiry.HashHelper.GetFileMD5(fileToUpload);
                    ShowMD5(md5);
                    ShowUploadStatus(0.00);
                });

                long filesize = 0;
                using (var fs = File.Open(fileToUpload, FileMode.Open))
                {
                    ed_size.Text = fs.Length.ToString();
                    if(fs.Length > (5000d * 1024 * 1024))
                    {
                        MessageBox.Show("系统暂时只支持5G以内视频,如有需要请联系开发商");
                        return;
                    }
                    filesize = fs.Length / 1024 / 1024;
                }
                this.ed_filesize.Text = "文件大小:" + filesize / 1000.0 + "G";
                var key = "Media/" + DateTime.Now.Ticks.ToString() + Path.GetExtension(fileToUpload);
                this.ed_url.Text = "http://files.zhejiangtashi.com/" + key;

                Task.Run(()=> {
                    OssUploader uploader = new OssUploader();
                    uploader.ShowUploadStatus = ShowUploadStatus;
                    uploader.Upload(fileToUpload,key);
                });
                
            }
        }

        private void ShowMD5(string md5)
        {
            Action action = () =>
            {
                this.ed_md5.Text = md5;
            };
            this.BeginInvoke(action);
        }

        private void ShowUploadStatus(double percent)
        {
            Action action = () =>
            {
                ed_ProgressBar.Value = (int)(percent * 100);
                ed_msg.Text = "已上传" + ((int)(percent * 100)).ToString() + "%";
            };
            this.BeginInvoke(action);
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            Task.Run(()=> {
                var info = new MediaInfo();
                info.name = this.ed_name.Text;
                info.size = double.Parse(ed_size.Text);
                info.mediaUrl = ed_url.Text;
                info.md5 = ed_md5.Text;
                info.startPlayTime = ed_startPlayTime.Value.ToShortDateString();
                info.effectiveTime = ed_startPlayTime.Value.ToShortDateString();
                info.endPlayTime = ed_endPlayTime.Value.ToShortDateString();

                info.format = "---";
                info.createTime = DateTime.Now.ToShortDateString();
                info.effectiveTime = DateTime.Now.ToShortDateString();
                info.time = "0";

                MediaApiService api = new MediaApiService(this.ServerBaseAddress);
                MessageBox.Show(api.DoPost(info).Result.InnerMessage);
            });
        }

        private void tsbLoadData_Click(object sender, EventArgs e)
        {
            Task.Run(()=> {
                MediaApiService api = new MediaApiService(ServerBaseAddress);
                var rtn = api.DoGet().Result;
                Action action = () =>
                {
                    this.bindingSource1.DataSource = rtn.Data;
                };
                this.BeginInvoke(action);
            });
            
        }
    }
}
